﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using AtletIS.Shared.Data;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Entities;

namespace AtletIS.Shared.Helpers
{
    public static class HtmlHelper
    {
        /// <summary>
        ///  Vygeneruje HTML head.
        /// </summary>
        public static void PrintHtmlHead(StreamWriter sw, string title)
        {
            sw.WriteLine("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
            sw.WriteLine("<html>");
            
            sw.WriteLine("<head>");
            sw.WriteLine("  <title>" + title +"</title>");
            sw.WriteLine("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1250\">");
            sw.WriteLine("  <link href=\"..\\Styles\\atletis.css\" rel=\"stylesheet\" type=\"text/css\">");
            sw.WriteLine("</head>");
        }

        /// <summary>
        ///  Vygeneruje header.
        /// </summary>
        public static void PrintHeader(StreamWriter sw, string title, CompetitionEntity competition)
        {
            sw.WriteLine("<div id=\"header\">");
            sw.WriteLine("  <img src=\"..\\Images\\header.png\" alt=\"Header\"/>");
            sw.WriteLine("  <hr>");
            sw.WriteLine("  <h1>" + competition.Name + "</h1>");
            sw.WriteLine("  " + competition.Info + "<br /><br />");
            sw.WriteLine("  <h2>" + title + "</h2>");
            sw.WriteLine("  Vygenerované " + DateTime.Now.ToShortDateString() + " o " + DateTime.Now.ToShortTimeString() + "");
            sw.WriteLine("</div>");
        }

        /// <summary>
        ///  Vygeneruje footer.
        /// </summary>
        public static void PrintFooter(StreamWriter sw)
        {
            sw.WriteLine("<div id=\"footer\">");
            sw.WriteLine("  <hr>");
            sw.WriteLine("  <b>Spracované systémom AtletIS</b><br>");
            sw.WriteLine("  Viac informácii na <a href=\"http://www.hrdosport.sk/\"><b>www.hrdosport.sk</b></a><br>");
            sw.WriteLine("  &copy HRDO šport " + DateTime.Now.Year.ToString() + "<br>");
            sw.WriteLine("</div>");
        }

        /// <summary>
        ///  Vygeneruje novy nazov pre HTML subor.
        /// </summary>
        public static string GetNewHtmlFileName(string prefix)
        {
            string tempPath = Path.Combine("Ext", "Temp");
            
            if (!Directory.Exists(tempPath))
                Directory.CreateDirectory(tempPath);

            #region Delete old files

            try
            {
                var files = Directory.GetFiles(tempPath);
                if (files != null && files.Length > 0)
                    foreach (var file in files)
                        File.Delete(file);
            }
            catch { }

            #endregion

            return Path.Combine("Ext", "Temp", prefix + " [" + Guid.NewGuid().ToString().Substring(0,8) + "].html");
        }
    }
}
